package com.lm.service;

import com.lm.mapper.MessageMapper;
import com.lm.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 发送消息
     */
    @Transactional
    public void sendMessage(String senderUid, String receiverUid, String content, Integer msgType) {
        Message message = new Message();
        message.setSenderUid(senderUid);
        message.setReceiverUid(receiverUid);
        message.setMsgContent(content);
        message.setMsgTime(new Date());
        message.setMsgType(msgType);
        message.setMsgIsread(0); // 默认未读
        messageMapper.insert(message);
    }

    /**
     * 获取两个用户之间的聊天记录
     */
    public List<Message> getChatHistory(String user1Uid, String user2Uid) {
        return messageMapper.getChatHistory(user1Uid, user2Uid);
    }

    /**
     * 获取用户的所有未读消息
     */
    public List<Message> getUnreadMessages(String uid) {
        return messageMapper.getUnreadMessages(uid);
    }

    /**
     * 标记消息为已读
     */
    @Transactional
    public void markAsRead(Integer messageId) {
        messageMapper.markAsRead(messageId);
    }

    /**
     * 获取用户的所有聊天对象
     */
    public List<String> getChatUsers(String uid) {
        return messageMapper.getChatUsers(uid);
    }

    /**
     * 获取与特定用户的未读消息数
     */
    public int getUnreadCount(String uid, String otherUid) {
        return messageMapper.getUnreadCount(uid, otherUid);
    }
} 